package com.itheima.health.jobs;

import com.itheima.health.common.RedisConst;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import java.util.Set;

@Slf4j
@Component //  定义任务实现类_定时清理图片

public class TimingCleanImg {
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private JedisPool jedisPool;

    @Scheduled(cron = "*/10 * * * * ?") //  5秒 直接在方法上使用注解即可完成扫描(cron = "0 0 3 1/1 * ? ") //  指定每天凌晨3点清除数据
    public  void  run(){
        log.info("执行扫描图片");
        //根据redis中保存的set集合与进行差值计算  获取垃圾图片的名称
        Set<String> GarbagePicture = jedisPool.getResource().sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);
        if (GarbagePicture != null) {
            for (String imgName : GarbagePicture) {
                //删除七牛的图片
                log.info("[删除七牛云图片]{}", imgName);
                qiniuUtils.deleteFileFromQiniu(imgName);
                //同时删除本地集合的数据
                log.info("[删除本地图片]{}", imgName);
                jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES, imgName);
            }
        }
    }
}


